import { useWxSDK } from "@/hooks/useWxSDK";
import { useCommonStore } from '@/store/common';
import axios from 'axios'
export function useWxShare(shareConfig: {
  configApi: string,
  hideShare: boolean,
  link: string,
  title?: string;
  imgUrl?: string;
  desc?: string;
}) {
  const store = useCommonStore();
  const { initConfig, setShareInfo } = useWxSDK();
  const url = location.href;
  const entryUrl = location.href.split('#')[0];
  // 进行签名的时候 Android 不用使用之前的链接， ios 需要
  let signLink = /(Android)/i.test(navigator.userAgent) ? location.href.split('#')[0] : (store.visitUrl || entryUrl);

  axios.post(shareConfig.configApi,{url:encodeURIComponent(signLink)}).then((config:any) => {
    initConfig(config?.data).then(() => {
      setShareInfo({
        ...shareConfig,
        link: shareConfig.link || url,
      });
    });
  });
}